package com.qimei.onerice.dao.impl;

import com.qimei.onerice.dao.CommentDao;
import com.qimei.onerice.entity.Comment;
import com.qimei.onerice.until.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class CommentDaoImpl implements CommentDao {

	private static final String INSERT_COMMENT = "INSERT INTO comment (comment_id, dish_id, customer_id, comment, comment_date) VALUES (?, ?, ?, ?, ?)";
	private static final String SELECT_COMMENT_BY_ID = "SELECT * FROM comment WHERE comment_id = ?";
	private static final String SELECT_COMMENTS_BY_DISH_ID = "SELECT * FROM comment WHERE dish_id = ?";
	private static final String UPDATE_COMMENT = "UPDATE comment SET dish_id = ?, customer_id = ?, comment = ?, comment_date = ? WHERE comment_id = ?";
	private static final String DELETE_COMMENT = "DELETE FROM comment WHERE comment_id = ?";

	@Override
	public int addComment(Comment comment) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(INSERT_COMMENT)) {
			preparedStatement.setInt(1, comment.getCommentId());
			preparedStatement.setInt(2, comment.getDishId());
			preparedStatement.setInt(3, comment.getCustomerId());
			preparedStatement.setString(4, comment.getComment());
			preparedStatement.setDate(5, comment.getCommentDate() != null ? new java.sql.Date(comment.getCommentDate().getTime()) : null);
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // Handle exception
		}
		return 0;
	}

	@Override
	public Comment getCommentById(int commentId) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_COMMENT_BY_ID)) {
			preparedStatement.setInt(1, commentId);
			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				if (resultSet.next()) {
					return createCommentFromResultSet(resultSet);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // Handle exception
		}
		return null;
	}

	@Override
	public List<Comment> getCommentListByDishId(int dishId) {
		List<Comment> comments = new ArrayList<>();
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_COMMENTS_BY_DISH_ID)) {
			preparedStatement.setInt(1, dishId);
			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				while (resultSet.next()) {
					comments.add(createCommentFromResultSet(resultSet));
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // Handle exception
		}
		return comments;
	}

	@Override
	public int updateComment(Comment comment) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_COMMENT)) {
			preparedStatement.setInt(1, comment.getDishId());
			preparedStatement.setInt(2, comment.getCustomerId());
			preparedStatement.setString(3, comment.getComment());
			preparedStatement.setDate(4, comment.getCommentDate() != null ? new java.sql.Date(comment.getCommentDate().getTime()) : null);
			preparedStatement.setInt(5, comment.getCommentId());
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // Handle exception
		}
		return 0;
	}

	@Override
	public int deleteComment(int commentId) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(DELETE_COMMENT)) {
			preparedStatement.setInt(1, commentId);
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // Handle exception
		}
		return 0;
	}

	// Helper method to create Comment object from ResultSet
	private Comment createCommentFromResultSet(ResultSet resultSet) throws SQLException {
		Comment comment = new Comment();
		comment.setCommentId(resultSet.getInt("comment_id"));
		comment.setDishId(resultSet.getInt("dish_id"));
		comment.setCustomerId(resultSet.getInt("customer_id"));
		comment.setComment(resultSet.getString("comment"));
		comment.setCommentDate(resultSet.getTimestamp("comment_date")); // Assuming it's stored as a Timestamp
		return comment;
	}
}
